package com.hzlx;

import com.hzlx.dto.InventoryRequest;
import com.hzlx.dto.InventoryResponse;
import com.hzlx.dto.ResultDto;
import jakarta.annotation.Resource;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/inventory")
public class InventoryController {
    @Resource
    RestTemplate restTemplate;
    @PostMapping("/isInStock")
    public ResultDto<List<InventoryResponse>> isInStockMap(@RequestBody List<Map<String,Object>> skus){
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity entity = new HttpEntity(skus,headers);
        ParameterizedTypeReference<ResultDto<List<InventoryResponse>>> pt = new ParameterizedTypeReference<>() {};
        return restTemplate.exchange("http://inventory-service/api/inventory/isInStock",
                HttpMethod.POST,
                entity, pt).getBody();
    }

    @ResponseStatus(HttpStatus.CREATED)
    @PostMapping
    public void create(@RequestBody InventoryRequest inventoryRequest){
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity entity = new HttpEntity(inventoryRequest,headers);
        restTemplate.postForLocation("http://inventory-service/api/inventory", entity);
    }

    @ResponseStatus(HttpStatus.OK)
    @PutMapping
    public void update(@RequestBody InventoryRequest request){
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity entity = new HttpEntity(request,headers);
        restTemplate.put("http://inventory-service/api/inventory", entity);
    }
}
